<?php

/*
	info: 用户信息读取及登录cookie设置
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

if (defined('MEMBER_CLASS'))
{
	return;
}
define('MEMBER_CLASS', TRUE);

class Member
{
	public $err = '';
	protected $db = null;
	protected $cookieName = 'prweddingid';
	protected $encodeKey = '';

	/**
	 * 构造函数
	 * @return void
	 */
	public function __construct($strCookieName = '')
	{
		global $db;
		global_ConnectDB();
		$this->db =& $db;
		$this->encodeKey = md5(WEBAUTHKEY);
		if ($strCookieName != '')
		{
			$this->SetCookieName($strCookieName);
		}
	}

	/**
	 * 检查用户帐号密码
	 * @param $strUserSign 用户名
	 * @param $strPassword 密码
	 * @return bool
	 */
	public function CheckUser($strUserSign = '', $strPassword = '')
	{
		global $user, $webcookie, $webserver, $webconfig, $db;
		$this->err = '';
		$strPassword = ($strPassword!='') ? md5($strPassword) : '';
		$intlogintime = $webserver['timestamp'];
		if ($strUserSign=='' && $strPassword=='' && isset($webcookie[$this->cookieName]))
		{
			$tempCookieArray = explode("\t", global_DecryptKey($webcookie[$this->cookieName], $this->encodeKey));
			if (count($tempCookieArray) == 3)
			{
				$strUserSign = $tempCookieArray[0];
				$strPassword = $tempCookieArray[1];
				$intlogintime = $tempCookieArray[2];
			}
		}
		$intlogintime = intval($intlogintime);
		$tempuserinfo = ($strUserSign!='') ? $db->GetOne("SELECT * FROM user WHERE email='".addslashes($strUserSign)."'") : array();
		if (!empty($tempuserinfo) && (strtolower($tempuserinfo['password'])==strtolower($strPassword) || strtolower(md5(strtolower(substr($tempuserinfo['password'],$tempuserinfo['uid']%9,6))))==strtolower($strPassword)))
		{
			$user['m_uid'] = $tempuserinfo['uid'];
			$user['m_truename'] = $tempuserinfo['truename'];
			$user['m_email'] = $tempuserinfo['email'];
			$user['m_mobile'] = $tempuserinfo['mobile'];
			$user['m_password'] = $tempuserinfo['password'];
			$user['m_weddingdate'] = $tempuserinfo['weddingdate'];
			$user['m_logintime'] = $intlogintime;
			return TRUE;
		}
		else
		{
			$this->err = '用户名或密码错误';
			if (isset($webcookie[$this->cookieName]))
			{
				$this->ClearUserCookie();
			}
		}
		return FALSE;
	}

	/**
	 * 清除用户登录cookie
	 * @return void
	 */
	public function ClearUserCookie()
	{
		global_ClearCookie($this->cookieName);
	}

	/**
	 * 设置登录cookie名称
	 */
	public function SetCookieName($strCookieName)
	{
		$this->cookieName = $strCookieName;
	}

	/**
	 * 设置用户登录cookie
	 * @param $strUserSign 用户名
	 * @param $strPassword 用户加密后的密码
	 * @return void
	 */
	public function SetUserCookie($strUserSign, $strPassword, $intTime = 0)
	{
		global $webserver;
		global_SetCookie($this->cookieName, global_EncryptKey($strUserSign."\t".$strPassword."\t".$webserver['timestamp'], $this->encodeKey), $intTime);
	}

}	//end class
